package com.gd.deron.smartrobot.baobao.db;


import java.util.ArrayList;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class DatabaseAsrDao {
	
	private DatabaseDBAsrOffineHelper helper;

	public DatabaseAsrDao(Context context){
		helper = new DatabaseDBAsrOffineHelper(context);
		helper.getWritableDatabase();
	}
	
	/**
	 * 保存成员信息
	 * @param gateWayList
	 */
	public void addQuestAndAnswer(String quest,String answer){
		SQLiteDatabase db = helper.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("question", quest);
		values.put("answer", answer);
		db.insert(DatabaseDBAsrOffineHelper.OFFLINE_TABLE_NAME, null, values);
		db.close();
	}
	
	
	/**
	 * 根据id更新账号信息
	 */
	public void updateQuestAndAnswer(String question,String answer){  
        SQLiteDatabase db = helper.getWritableDatabase();  
        ContentValues values = new ContentValues();  
		values.put("question", question);
		values.put("answer", answer);
        db.update(DatabaseDBAsrOffineHelper.OFFLINE_TABLE_NAME, values, "question=?", new String[]{question});  
    }
	
	/**
	 * 
	 * @return
	 */
	public String findQuestToAnswer(String question){
		SQLiteDatabase db = helper.getWritableDatabase();
		String answer = null;
	//	ArrayList<DatabaseAsrOfflineBean> memberList = new ArrayList<DatabaseAsrOfflineBean>();
		Cursor cursor = db.rawQuery("select * from "+DatabaseDBAsrOffineHelper.OFFLINE_TABLE_NAME+" where question=?", new String[]{question});
		for (int i = 0; i < cursor.getCount(); i++) {
			cursor.moveToNext();
			//DatabaseAsrOfflineBean info = new DatabaseAsrOfflineBean();
			//info.setQuestion(cursor.getString(cursor.getColumnIndex("question")));
			//info.setAnswer(cursor.getString(cursor.getColumnIndex("answer")));
			answer = cursor.getString(cursor.getColumnIndex("answer"));
			//memberList.add(info);
		}
		cursor.close();
		db.close();
		return answer;
	}
	/**
	 * 查询全部的问题和答案
	 * @return
	 */
	public ArrayList<DatabaseAsrOfflineBean> findAllQuestToAnswer(){
		SQLiteDatabase db = helper.getWritableDatabase();
		ArrayList<DatabaseAsrOfflineBean> memberList = new ArrayList<DatabaseAsrOfflineBean>();
		Cursor cursor = db.rawQuery("select * from "+DatabaseDBAsrOffineHelper.OFFLINE_TABLE_NAME, null);
		for (int i = 0; i < cursor.getCount(); i++) {
			cursor.moveToNext();
			DatabaseAsrOfflineBean info = new DatabaseAsrOfflineBean();
			info.setQuestion(cursor.getString(cursor.getColumnIndex("question")));
			info.setAnswer(cursor.getString(cursor.getColumnIndex("answer")));
			memberList.add(info);
		}
		cursor.close();
		db.close();
		return memberList;
	}
	
	/**
	 * 根据成员id删除成员
	 * @param id
	 */
	public void deleteByQuest(String question){
		SQLiteDatabase db = helper.getWritableDatabase();
		db.execSQL("delete from "+DatabaseDBAsrOffineHelper.OFFLINE_TABLE_NAME+" where user_id=?", new String[]{question}); 
	}
	/**
	 * 删除全部成员
	 * @param id
	 */
	public void deleteAllByQuest(){
		SQLiteDatabase db = helper.getWritableDatabase();
		db.execSQL("delete from "+DatabaseDBAsrOffineHelper.OFFLINE_TABLE_NAME); 
	}

	
}
